package com.luych.toolbox.common.service.tool;

public class Capacity {

    public static long convertToB(String capacity) {
        capacity = capacity.toUpperCase();
        if (capacity.endsWith("KB") || capacity.endsWith("K")) {
            capacity = capacity.replace("KB", "").replace("K", "").trim();
            return Long.parseLong(capacity) * 1024;
        } else if (capacity.endsWith("MB") || capacity.endsWith("M")) {
            capacity = capacity.replace("MB", "").replace("M", "").trim();
            return Long.parseLong(capacity) * 1024 * 1024;
        } else if (capacity.endsWith("GB") || capacity.endsWith("G")) {
            capacity = capacity.replace("GB", "").replace("G", "").trim();
            return Long.parseLong(capacity) * 1024 * 1024 * 1024;
        } else if (capacity.endsWith("TB") || capacity.endsWith("T")) {
            capacity = capacity.replace("TB", "").replace("T", "").trim();
            return Long.parseLong(capacity) * 1024 * 1024 * 1024 * 1024;
        } else if (capacity.endsWith("PB") || capacity.endsWith("P")) {
            capacity = capacity.replace("PB", "").replace("P", "").trim();
            return Long.parseLong(capacity) * 1024 * 1024 * 1024 * 1024 * 1024;
        } else if (capacity.endsWith("EB") || capacity.endsWith("E")) {
            capacity = capacity.replace("EB", "").replace("E", "").trim();
            return Long.parseLong(capacity) * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
        } else if (capacity.endsWith("ZB") || capacity.endsWith("Z")) {
            capacity = capacity.replace("ZB", "").replace("Z", "").trim();
            return Long.parseLong(capacity) * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
        } else if (capacity.endsWith("B")) {
            capacity = capacity.replace("B", "").trim();
            return Long.parseLong(capacity);
        }  else {
            return Long.parseLong(capacity);
        }
    }

    public static double convertToKB(String capacity) {
        return Math.round(convertToB(capacity) * 1000D / 1024D) / 1000D;
    }

    public static double convertToMB(String capacity) {
        return Math.round(convertToB(capacity) * 1000D / 1024D / 1024D) / 1000D;
    }

    public static double convertToGB(String capacity) {
        return Math.round(convertToB(capacity) * 1000D / 1024D / 1024D / 1024D) / 1000D;
    }

    public static String convertHumanReadFull(long  b) {
        long tb = b / 1024L / 1024L / 1024L / 1024L;
        long gb = ((b % (1024L * 1024L * 1024L * 1024L)) / (1024L * 1024L * 1024L));
        long mb = ((b % (1024L * 1024L * 1024L)) / (1024L * 1024L));
        long kb = ((b % (1024L * 1024L)) / 1024L);
        long bb = (b % 1024L);
        return (tb > 0 ? (tb + "TB") : "") +
                (gb > 0 ? (gb + "GB") : "") +
                (mb > 0 ? (mb + "MB") : "") +
                (kb > 0 ? (kb + "KB") : "") +
                (bb > 0 ? (bb + "B") : "");
    }

    public static String convertHumanReadSimple(long  b) {
        if (b >= 1024L * 1024L * 1024L * 1024L) return Math.round(b / 1024F / 1024F / 1024F / 1024F * 100F) / 100F + "TB";
        if (b >= 1024L * 1024L * 1024L) return Math.round(b / 1024F / 1024F / 1024F * 100F) / 100F + "GB";
        if (b >= 1024L * 1024L) return Math.round(b / 1024F / 1024F * 100F) / 100F + "MB";
        if (b >= 1024L) return Math.round(b / 1024F * 100F) / 100F + "KB";
        return b + "B";
    }

}
